// pages/pay/moreAddress/inserAddress/index.js
import { sendServer } from '../../../../request/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receName:'',
    recePhone:"",
    address:'',
    postcode:'',
    detail:"",
    status:"",
    region: ['省', '市', '区'],
    customItem: '全部'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow: function () {

  },
  nameInput(e){
    let value=e.detail
    this.setData({
      receName:value
    })
  },
  phoneInput(e){
    let value=e.detail
    this.setData({
      recePhone:value
    })
  },
  codeInput(e){
    let value=e.detail
    this.setData({
      postcode:value
    })
  },
  detailInput(e){
    let value=e.detail
    this.setData({
      detail:value
    })
  },
  enableStockChange: function (options) {
    var status = '0';
    if (options.detail.value) {
      status = '1';
    }
    this.setData({
      status: status
    })
  
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  addAddress(){
    let params={},send={};
    let status=this.data.status || ''
    if(status==''){
      status='0'
    }else{
      status='1'
    }
    send.userId=wx.getStorageSync('userInfo').id;
    let address=this.data.region[0]+this.data.region[1]+this.data.region[2];
    send.address=address
    send.detail=this.data.detail
    send.postcode=this.data.postcode
    send.receName=this.data.receName
    send.recePhone=this.data.recePhone
    send.status=status;
    params.send=send;
    params.url="address/addAddress"
    console.log(send)
    sendServer(params,(commRes)=>{
      if(commRes.code==100){
          wx.showToast({
            title: '新增成功',
            icon:'success',
            duration:3000
          })
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.setData({
            refresh:true
          }) 
          wx.navigateBack({
            delta: 1,
          })
      }else{
        wx.showToast({
          title: '新增失败',
          icon:'none'
        })
      }
    })
  },
  
})